<script setup>
import { artGetChannelsService, artDelChannelService } from '@/api/article'
import ChannelEdit from './components/ChannelEdit.vue'
import { Edit, Delete } from '@element-plus/icons-vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { ref } from 'vue'
const channelList = ref([])
const dialog = ref()
const isLoading = ref(false)
const artGetChannelList = async () => {
  // 加载
  isLoading.value = true
  const res = await artGetChannelsService()
  // 关闭加载
  isLoading.value = false
  channelList.value = res.data.data
  console.table(channelList.value)
}
artGetChannelList()

// 删除
const onDelChannel = async (row) => {
  isLoading.value = true
  await ElMessageBox.confirm('确认要删除该分类吗', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认删除',
    cancelButtonText: '不删除'
  })
  await artDelChannelService(row.id)
  ElMessage.success('删除成功~')
  isLoading.value = false
  // 刷新
  artGetChannelList()
}
// 表格编辑
const onEditChannel = (row) => {
  dialog.value.open(row)
}
// 添加分类
const onAddChannel = () => {
  dialog.value.open({})
}
const onSuccess = () => {
  artGetChannelList()
}
</script>

<template>
  <PageContainer title="文章分类">
    <template #extra
      ><el-button type="primary" @click="onAddChannel"
        >添加分类</el-button
      ></template
    >

    <el-table v-loading="isLoading" :data="channelList" style="width: 100%">
      <el-table-column type="index" label="序号" width="150"></el-table-column>
      <el-table-column label="分类名称" prop="cate_name"></el-table-column>
      <el-table-column label="分类别名" prop="cate_alias"></el-table-column>
      <el-table-column label="操作" width="100">
        <!-- row 就是 channelList的每一项  $index就是索引-->
        <template #default="{ row, $index }">
          <el-button
            type="primary"
            plain
            :icon="Edit"
            circle
            @click="onEditChannel(row, $index)"
          ></el-button>
          <el-button
            type="danger"
            plain
            :icon="Delete"
            circle
            @click="onDelChannel(row, $index)"
          ></el-button>
        </template>
      </el-table-column>
      <!-- 空状态 -->
      <template #empty>
        <el-empty description="暂无数据"></el-empty>
      </template>
    </el-table>
    <ChannelEdit ref="dialog" @success="onSuccess"></ChannelEdit>
  </PageContainer>
</template>
